package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.FamilyBiz;
import cn.sumpu.app.guoguojie.biz.FriendBiz;
import cn.sumpu.app.guoguojie.biz.MailBiz;
import cn.sumpu.app.guoguojie.constant.FamilyRole;
import cn.sumpu.app.guoguojie.constant.GroupClassify;
import cn.sumpu.app.guoguojie.constant.Sex;
import cn.sumpu.app.guoguojie.dao.DetailDao;
import cn.sumpu.app.guoguojie.dao.FamilyDao;
import cn.sumpu.app.guoguojie.dao.MemberDao;
import cn.sumpu.app.guoguojie.domain.Detail;
import cn.sumpu.app.guoguojie.domain.Family;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.DoorplateGenerator;
import cn.sumpu.app.guoguojie.util.IdGenerator;

public class FamilyBizImpl implements FamilyBiz {

	private FamilyDao familyDao;
	private MemberDao memberDao;
	private DetailDao detailDao;
	@Autowired
	private FriendBiz friendBiz;

	private MailBiz mailBiz;

	public void setDetailDao(DetailDao detailDao) {
		this.detailDao = detailDao;
	}

	public void setFamilyDao(FamilyDao familyDao) {
		this.familyDao = familyDao;
	}

	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}

	public void setMailBiz(MailBiz mailBiz) {
		this.mailBiz = mailBiz;
	}

	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public String register(Family family, Member member)
			throws AppBusinessException {
		String doorplate = DoorplateGenerator.createDoorplate();
		family.setDoorplate(doorplate);
		family.setIsActive(true);
		member.setFamily(doorplate);
		String passwd = member.getPassword();
		String salt = Common.getRandStr(6, false);
		member.setSalt(salt);
		member.setPassword(Common.md5(Common.md5(passwd)));
		String identifier = IdGenerator.createIdentifier();
		member.setIdentifier(identifier);
		familyDao.insert(family);
		memberDao.insert(member);
		Detail detail = new Detail();
		detail.setMember(member.getIdentifier());
		detail.setEmail(member.getEmail());
		detailDao.insertDetail(detail);
		return doorplate;
	}

	@Override
	public Family getFamilyByDoor(String doorplate) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("doorplate", doorplate);
		List<Family> fList = familyDao.getFamilyList(map);
		if (fList != null && fList.size() > 0)
			return fList.get(0);
		return null;
	}

	@Override
	public void updateFamily(Family family) throws AppBusinessException {
		familyDao.updateFamily(family);
	}

	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public Member register(String basePath, Family family, Member member,
			List<Member> babyList) throws AppBusinessException {
		String doorplate = DoorplateGenerator.createDoorplate();
		String identifier = IdGenerator.createIdentifier();

		family.setDoorplate(doorplate);
		family.setAvatar(Common.AVATAR_FAMILY_DEFALUT);
		familyDao.insert(family);

		member.setFamily(doorplate);
		member.setIdentifier(identifier);
		member.setPassword(Common.md5(Common.md5(member.getPassword())));
		member.setActiveCode(Common.getRandStr(10, false));
		member.setIsHost(true);
		if (FamilyRole.FATHER.getValue().equals(member.getRole())) {
			member.setAvatar(Common.AVATAR_MAN_DEFALUT);
		} else if (FamilyRole.MOTHER.getValue().equals(member.getRole())) {
			member.setAvatar(Common.AVATAR_WOMAN_DEFALUT);
		}
		memberDao.insert(member);

		member.setFamilyObj(family);
		//
		Detail detailMember = new Detail();
		detailMember.setProvice(family.getProvice());
		detailMember.setCity(family.getCity());
		detailMember.setDistrict(family.getDistrict());
		detailMember.setMember(identifier);
		// 如果是妈妈角色则将性别是女，否则是男
		detailMember
				.setSex(FamilyRole.MOTHER.getValue().equals(member.getRole()) ? Sex.FEMALE
						.getValue() : Sex.MALE.getValue());
		detailDao.insertDetail(detailMember);

		member.setDetail(detailMember);

		if (babyList != null && babyList.size() > 0) {
			for (Member baby : babyList) {
				String babyId = IdGenerator.createIdentifier();
				baby.setIdentifier(babyId);
				baby.setFamily(doorplate);
				baby.setRole(FamilyRole.BABY.getValue());
				baby.setAvatar(Common.AVATAR_BABY_DEFALUT);
				memberDao.insert(baby);
				Detail detail = new Detail();
				detail.setMember(babyId);
				detail.setSex(baby.getSex());
				detail.setBirthday(baby.getBirthday());
				detailDao.insertDetail(detail);
			}
		}
		// 新建家时添加默认的五个分组
		friendBiz.createGroup(member.getFamily(), GroupClassify.FRIEND.getLabel(),GroupClassify.FRIEND.getValue(), null);
		friendBiz.createGroup(member.getFamily(), GroupClassify.RELATIVE.getLabel(),GroupClassify.RELATIVE.getValue(), null);
		friendBiz.createGroup(member.getFamily(), GroupClassify.SCHOOLMATE.getLabel(),GroupClassify.SCHOOLMATE.getValue(), null);
		friendBiz.createGroup(member.getFamily(), GroupClassify.COLLEAGUE.getLabel(),GroupClassify.COLLEAGUE.getValue(), null);
		friendBiz.createGroup(member.getFamily(), GroupClassify.LITTLEAREA.getLabel(),GroupClassify.LITTLEAREA.getValue(), null);

		mailBiz.sendEmail(basePath, member);
		return member;
	}

	@Override
	public boolean activeMember(String memberId, String activeCode)
			throws AppBusinessException {
		Member originalM = memberDao.getMemberById(memberId);
		if (null != originalM && activeCode.equals(originalM.getActiveCode())) {
			Member updateM = new Member();
			updateM.setIdentifier(memberId);
			updateM.setIsActive(true);
			memberDao.updateMember(updateM);
			return true;
		}
		return false;
	}

	// 通过会员编号查询得到家的信息
	@Override
	public Family getFamilyByMember(String member) throws AppBusinessException {
		return familyDao.getFamilyByMember(member);
	}

}
